#include <cstddef>
#include <cstring>
#include <netinet/in.h>
#include <string>
#include <sys/socket.h>
#include <utility>

#include "../../base/Defind.hpp"

class Msg;

class CliConn {
public:
    CliConn(const char *ip, int port);
    ~CliConn();

    void echo(Msg &msg);

private:
    int fd;
    struct sockaddr_in sock;
    socklen_t len;
};

class Msg {
public:
    Msg(const char *data, short data_len)
    :   len(data_len + 1 + HEAD_LEN),
        msg(new char[data_len + HEAD_LEN + 1])
    {
        memset(msg, 0, data_len + HEAD_LEN + UUID_LEN + 1);
        memcpy(msg, &data_len, HEAD_LEN);
        memcpy(msg + HEAD_LEN, data, data_len);
        msg[data_len + HEAD_LEN] = 0;
    }

    auto operator()() -> std::pair<short, const char *> { 
        return {len, msg}; 
    }

    ~Msg() { delete[] msg; }

private:
    short len;
    char *msg;
};